//搜索界面
//  OpSearchViewController.m
//  LoveFreshBeeProject
//
//  Created by skywalker on 16/12/31.
//  Copyright © 2016年 斯芬克斯. All rights reserved.
//

#import "OpSearchViewController.h"
#import "PYSearch.h"
@interface OpSearchViewController ()<PYSearchViewControllerDelegate>
@property(nonatomic,strong)UISearchBar * searchBar;
@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSArray * hotList;
@end

@implementation OpSearchViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    
    _tableView=[[UITableView alloc]init];
    _tableView.backgroundColor=[UIColor colorWithRed:239/255.0 green:239/255.0 blue:239/255.0 alpha:1];
    [_tableView setSeparatorColor:[UIColor clearColor]];
    
    [self.view addSubview:_tableView];
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
   
    UISearchBar * searchbar=[[UISearchBar alloc]init];
    searchbar.placeholder=@"请输入商品名称";
    searchbar.showsBookmarkButton=YES;
    [searchbar setKeyboardType:UIKeyboardTypeDefault];
    self.navigationItem.titleView=searchbar;
    //读值
         NSArray *hotSeaches = @[@"年货大集", @"酸奶", @"水", @"车厘子", @"洽洽瓜子", @"维他", @"香烟", @"周黑鸭", @"草莓", @"星巴克", @"卤味"];
//    NSArray * hotSeaches =[self loadHotList];
    PYSearchViewController * searchViewController =[PYSearchViewController searchViewControllerWithHotSearches:hotSeaches searchBarPlaceholder:@"热门商品" didSearchBlock:^(PYSearchViewController *searchViewController, UISearchBar *searchBar, NSString *searchText) {
        // 开始搜索执行以下代码
        // 如：跳转到指定控制器
//        [searchViewController.navigationController pushViewController:[[PYTempViewController alloc] init] animated:YES];
    }];
    searchViewController.hotSearchStyle = PYHotSearchStyleColorfulTag; // 热门搜索风
    searchViewController.delegate = self;
    [self.view addSubview:searchViewController.view];
}
#pragma mark -代理方法
- (void)searchViewController:(PYSearchViewController *)searchViewController searchTextDidChange:(UISearchBar *)seachBar searchText:(NSString *)searchText
{
    if (searchText.length) { // 与搜索条件再搜索
        // 根据条件发送查询（这里模拟搜索）
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.25 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ // 搜素完毕
            // 显示建议搜索结果
            NSMutableArray *searchSuggestionsM = [NSMutableArray array];
            for (int i = 0; i < arc4random_uniform(5) + 10; i++) {
                NSString *searchSuggestion = [NSString stringWithFormat:@"搜索建议 %d", i];
                [searchSuggestionsM addObject:searchSuggestion];
            }
            // 返回
            searchViewController.searchSuggestions = searchSuggestionsM;
        });
    }
}
#pragma mark -加载数据

-(NSArray *)loadHotList{
    AFHTTPSessionManager * manager=[AFHTTPSessionManager manager];
    // 增加AFN支持的文件类型
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/json", @"text/json", @"text/javascript", @"text/html", @"text/plain", nil];
    NSDictionary *parameters = [NSDictionary dictionaryWithObject:@"6" forKey:@"call"];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    // 修改AFN默认的发送数据的类型,改为JOSN
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager POST:@"http://iosapi.itcast.cn/loveBeen/search.json.php" parameters:parameters progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        //                NSLog(@"%@",responseObject);
        NSString *str = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"%@",str);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];
    // 1.获取文件路径
    NSString *jsonPath = [[NSBundle mainBundle] pathForResource:@"search.json" ofType:nil];
    // 2.获取文件二进制
    NSData *jsonData = [NSData dataWithContentsOfFile:jsonPath];
    // 3.反序列化
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL];
    // 4.取出tList对应的字典数组
    NSArray *dictArr = result[@"hotquery"];
    return dictArr;
}
- (void)viewWillAppear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = YES;
    
}
- (void)viewWillDisappear:(BOOL)animated{
    
    self.tabBarController.tabBar.hidden = NO;
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
